package com.yuerlive.rainbow.module.mine.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.common.BaseRecycleViewAdapter;
import com.yuerlive.rainbow.entity.MessageInfo;
import com.yuerlive.rainbow.entity.MyMsgInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017/5/18.
 */

public class MyMessageAdapter extends BaseRecycleViewAdapter {
    List<MyMsgInfo> mDatas;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_EMPTY = 1;

    public MyMessageAdapter(Context mContext, List<MyMsgInfo> datas) {
        super(mContext);
        this.mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_EMPTY)
            return new EmptyHolder(mInflater.inflate(R.layout.layout_exception, parent, false));
        return new ItemHolder(mInflater.inflate(R.layout.layout_message_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_ITEM) {
            setItemData((ItemHolder) holder, position);
        }
    }

    private void setItemData(ItemHolder holder, int position) {
        MyMsgInfo myMsgInfo = mDatas.get(position);
        if (myMsgInfo != null) {
            holder.tvTitle.setText(myMsgInfo.getTitle());
            holder.tvContent.setText(myMsgInfo.getContent());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas == null || mDatas.isEmpty()) {
            return TYPE_EMPTY;
        }
        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        if (mDatas.size() == 0) {
            return 1;
        }
        return mDatas.size();
    }
    static  class  EmptyHolder extends RecyclerView.ViewHolder{
        public EmptyHolder(View itemView){
            super(itemView);
        }
    }
    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imgNext)
        ImageView imgNext;
        @BindView(R.id.tvTitle)
        TextView tvTitle;
        @BindView(R.id.tvContent)
        TextView tvContent;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(v -> {
                if (mItemClickListener != null)
                    mItemClickListener.onItemClick(v, getLayoutPosition());
            });
        }
    }
}
